﻿using Furion;
using Furion.Schedule;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

Serve.Run(s =>
{
    s.AddSchedule(options =>
    {
        // 注册作业，并配置作业触发器
        options.AddJob<MyJob>("job1",Triggers.Secondly()); // 表示每秒执行
    });
});

[DynamicApiController]
public class apiAction
{
    public string changeTrigger()
    {
        //如何正确操作变动触发器间隔时间
        
        var schedulerFactory = App.GetService<ISchedulerFactory>();
        var scheduleResult = schedulerFactory.TryGetJob("job1", out var scheduler);
           
        var builderTrigger = scheduler.GetModel().Triggers[0];


        var triggerBuilder = Triggers.PeriodSeconds(5);
        triggerBuilder.SetTriggerId(builderTrigger.TriggerId);
           
        triggerBuilder.Updated();

        scheduler.UpdateTrigger(triggerBuilder);
        
        return "变动触发器间隔";
    }
}


public class MyJob : IJob
{
    private readonly ILogger<MyJob> _logger;

    public MyJob(ILogger<MyJob> logger)
    {
        _logger = logger;
    }

    public Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        _logger.LogInformation($"{context}");
        return Task.CompletedTask;
    }
}